//获取二级分类下 商品列表的数据
import { reqSubGoodList } from "@/apis/category";
import { ref, onMounted } from "vue";
import { useRoute } from "vue-router";
export const useGoodList = () => {
  //获取当前路由实例
  let route = useRoute();
  //获取商品列表时需要携带的参数
  let goodsParams = ref({
    categoryId: route.params.id,
    page: 1,
    pageSize: 20,
    sortField: "publishTime",
  });
  //禁止加载数据
  let disabled = ref(false);
  //获取基础列表数据的数组
  let goodsList = ref([]);
  onMounted(() => {
    getGoodsList();
  });
  // 获取基础列表数据的回调
  const getGoodsList = async (params = goodsParams.value) => {
    let res = await reqSubGoodList(params);
    // console.log(res);
    if (goodsParams.value.page == 1) {
      goodsList.value = res.result.items;
    }
    //加载完毕 停止监听
    if (res.result.items.length == 0) {
      console.log("数据加载完毕,停止监听");
      disabled.value = true;
    }
    //新老数据拼接
    goodsList.value = [...goodsList.value, ...res.result.items];
  };
  return {
    goodsList,
    getGoodsList,
    goodsParams,
    disabled,
  };
};
